package cn.tedu.sp04.order.controller;

import cn.tdeu.sp01.pojo.Item;
import cn.tdeu.sp01.pojo.Order;
import cn.tdeu.sp01.pojo.User;
import cn.tdeu.sp01.service.OrderService;
import cn.tdeu.sp01.web.util.JsonResult;

import cn.tedu.sp04.feign.ItemClient;
import cn.tedu.sp04.feign.UserClient;

import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RestController;

import java.util.Arrays;
import java.util.List;

@RestController
@Slf4j
public class OrderController {
    @Autowired
    private OrderService orderService;

    // 获取订单
    @GetMapping("/{orderId}")
    public JsonResult<Order> getOrder(@PathVariable String orderId) {
        Order order = orderService.getOrder(orderId);



        return JsonResult.ok().data(order);
    }
    // 保存订单
    @GetMapping("/")
    public JsonResult<?> saveOrder() {
        // 写死订单数据
        Order order = new Order();
        order.setId("i7uj54h6tg346u");
        order.setUser(new User(8,null,null)); //实际项目中，要获取已登录的用户id
        order.setItems(Arrays.asList(new Item[]{
                new Item(1,"商品1",1),
                new Item(2,"商品2",3),
                new Item(3,"商品3",5),
                new Item(4,"商品4",2),
                new Item(5,"商品5",6),
        }));

        orderService.saveOrder(order);
        return JsonResult.ok().msg("保存订单成功");
    }

}
